// eslint-disable-next-line
import { useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import Creator from "../store/actions";
import AdminsApi from '../api/admins';

export const useAdmin = () => {
    // 提取数据  useSelector共享状态,从Redux的store中提取数据
    const oneAdmins = useSelector(state => {
        return state.oneAdmins
    });
    const dispatch = useDispatch();
    //修改数据
    const changeAdmins = useCallback(async ({adminName, phone,adminPassword , _id}) => {
        const data = await AdminsApi.change({ adminName, phone,adminPassword , _id})
        return data
    }, [])

    // 获取_id对应数据
    const getAdminsId = useCallback(async _id => {
        const data = await AdminsApi.getOne({ _id })
        dispatch(Creator.getAdminsId({ data }))
    }, [dispatch])

    return [oneAdmins, {  changeAdmins, getAdminsId }]
}